View Javadoc

1   package uba.db.sql.server;
2   
3   /***
4    * Modelo de la máquina de estados que representa el comportamiento del
5    * protocolo a usar.
6    * 
7    * @version $Revision$
8    */
9   public class DBProtocol {
10  	private static final int WAITING = 0;
11  	private static final int LISTENING = 1;
12  	private int state = WAITING;
13      private ServerProcessor server;
14  
15  	public DBProtocol(ServerProcessor serverProcessor) {
16          this.server = serverProcessor;
17      }
18  
19      /***
20  	 * Procesa un string y decide que mandar por el socket.
21  	 * 
22  	 * @param theInput
23  	 *            El string recibido.
24  	 * @return Comandos de control o una tabla en formato listo para ser pasado
25  	 *         por el socket.
26  	 */
27  	public String processInput(String theInput) {
28  		String theOutput = null;
29  
30  		if (state == WAITING) {
31  			theOutput = "Init";// "Ready";
32  			state = LISTENING;
33  		} else if (state == LISTENING) {
34  			theOutput = getResponseFromServer(theInput);
35  		} else {
36  			theOutput = "Connection Closed";// "Session Finished";
37  			state = WAITING;
38  		}
39  		return theOutput;
40  	}
41  
42  	/***
43  	 * Obtiene el String a pasar por el socket.
44  	 * 
45  	 * @param s
46  	 *            La sentencia a procesar.
47  	 * @return Una respuesta para enviar por el socket.
48  	 */
49  	public String getResponseFromServer(String s) {
50  		if (s.equalsIgnoreCase("close"))
51  			return "Connection Closed";
52  		String result = server.execute(s);
53  		return result;
54  	}
55  }